package com.javaAdmin.util;


import com.javaAdmin.enums.ResultCodeEnum;

/**
 * @author zengjintao
 * @version 1.0.6
 * @create_at 2022/11/13 21:10
 */
public class Result<T> {

    /**
     * 响应状态码code
     */
    private Integer code;

    /**
     * 响应消息提示
     */
    private String msg;

    /**
     * 响应数据
     */
    private T data;


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    private Result(ResultCodeEnum codeEnum, T data) {
        this.code = codeEnum.getCode();
        this.msg = codeEnum.getMsg();
        this.data = data;
    }

    private Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 响应成功
     *
     * @param <T>
     * @return
     */
    public static <T> Result success() {
        return new Result(ResultCodeEnum.SUCCESS, null);
    }

    /**
     * 响应成功
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result success(T data) {
        return new Result(ResultCodeEnum.SUCCESS, data);
    }

    /**
     * 响应成功
     *
     * @param codeEnum
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result success(ResultCodeEnum codeEnum, T data) {
        return new Result(codeEnum, data);
    }

    /**
     * 响应失败
     *
     * @return
     */
    public static Result fail() {
        return new Result(ResultCodeEnum.FAIL, null);
    }


    /**
     * 响应失败
     *
     * @param errorMsg
     * @return
     */
    public static Result fail(String errorMsg) {
        return new Result(ResultCodeEnum.FAIL.getCode(), errorMsg);
    }
}
